package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2024-06-18 16:33:41
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo> implements RoomInfoService {

    @Autowired
    RoomInfoMapper roomInfoMapper;

    @Autowired
    RoomFacilityService roomFacilityService;
    @Autowired
    RoomLabelService roomLabelService;
    @Autowired
    RoomAttrValueService roomAttrValueService;
    @Autowired
    RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    RoomLeaseTermService roomLeaseTermService;
    @Autowired
    GraphInfoService graphInfoService;
    @Autowired
    ProvinceInfoService provinceInfoService;
    @Autowired
    CityInfoService cityInfoService;
    @Autowired
    DistrictInfoService districtInfoService;
    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;


    //分页查询房间
    @Override
    public IPage<RoomItemVo> pageItem(IPage<RoomItemVo> iPage, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(iPage, queryVo);
    }

    //保存|修改房间信息
    @Override
    public void saveOrUpdateInfo(RoomSubmitVo roomSubmitVo) {

        //根据id判断是不是修改行为
        Long roomId = roomSubmitVo.getId();
        boolean flag = false;
        if (roomId != null) {
            flag = true;
        }


        //保存更新房间信息
        saveOrUpdate(roomSubmitVo);

        if(flag){    //是修改，先删除房间关联的其他外键表数据

            //1.删除原有的graphInfoList
            graphInfoService.remove
                    (new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,roomSubmitVo.getId())
                            .eq(GraphInfo::getItemType,ItemType.ROOM));

            //2.删除原有roomAttrValueList
            roomAttrValueService.remove
                    (new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,roomSubmitVo.getId()));

            //3.删除原有roomFacilityList
            roomFacilityService.remove
                    (new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getFacilityId,roomSubmitVo.getId()));

            //4.删除原有roomLabelList
            roomLabelService.remove
                    (new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getLabelId,roomSubmitVo.getId()));

            //5.删除原有paymentTypeList
            roomPaymentTypeService.remove
                    (new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getPaymentTypeId,roomSubmitVo.getId()));

            //6.删除原有leaseTermList
            roomLeaseTermService.remove
                    (new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getLeaseTermId,roomSubmitVo.getId()));

        }


        //保存新的房间信息

        //配套  facilityInfoIds：所有配套信息的id
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            List<RoomFacility> roomFacilityList = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(facilityInfoId).build();
                roomFacilityList.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilityList);
        }


        //标签
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)) {
            List<RoomLabel> roomLabelList = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelInfoId).build();
                roomLabelList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelList);
        }

        //图片
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }


        //属性
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)) {
            List<RoomAttrValue> roomAttrValueList = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build();
                roomAttrValueList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValueList);
        }

        //租期
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)) {
            ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build();
                roomLeaseTerms.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }
        //支付方式
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)) {
            ArrayList<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build();
                roomPaymentTypeList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }
    }

    //回显
    @Override
    public RoomDetailVo getByIdInfo(Long id) {

        RoomDetailVo roomDetailVo=new RoomDetailVo();

        //查询房间基本信息
        RoomInfo roomInfo = getById(id);

        //查询所属公寓信息
        ApartmentInfo apartmentInfo=apartmentInfoMapper.selectById(roomInfo.getApartmentId());

        //查询房间配套
        List<FacilityInfo> roomFacilityList = roomFacilityService.getRoomFacilityInfo(id);

        //查询房间标签
        List<LabelInfo> roomLabelInfo=roomLabelService.getRoomLabelInfo(id);

        //查询房间租期
        List<LeaseTerm> roomLeaseTermList=roomLeaseTermService.getRoomLeaseTermInfo(id);

        //查询房间支付方式
        List<PaymentType> roomPaymentTypeList=roomPaymentTypeService.getRoomPaymentTyperInfo(id);

        //查询房间属性
        List<AttrValueVo> roomAttrValueList=roomAttrValueService.getRoomAttrValue(id);

        //查询房间图片
        List<GraphVo> roomGraphList=graphInfoService.getRoomGraphInfo(ItemType.ROOM,id);

        BeanUtils.copyProperties(roomInfo,roomDetailVo);

        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(roomGraphList);
        roomDetailVo.setFacilityInfoList(roomFacilityList);
        roomDetailVo.setAttrValueVoList(roomAttrValueList);
        roomDetailVo.setLabelInfoList(roomLabelInfo);
        roomDetailVo.setPaymentTypeList(roomPaymentTypeList);
        roomDetailVo.setLeaseTermList(roomLeaseTermList);
        return roomDetailVo;
    }


    //删除房间
    @Override
    public void removeRoomById(Long id) {
        //删除RoomInfo
        removeById(id);

        //2.删除graphInfoList
        graphInfoService.remove
                (new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,id).eq(GraphInfo::getItemType,ItemType.ROOM));

        //3.删除attrValueList
        roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,id));

        //4.删除facilityInfoList
        roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getFacilityId,id));

        //5.删除labelInfoList
        roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getLabelId,id));

        //6.删除paymentTypeList
        roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getPaymentTypeId, id));

        //7.删除leaseTermList
        roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getLeaseTermId,id));
    }
}




